﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace UCF.Core.Web.Demo
{
    /// <summary>
    /// 演示会话购物车类 [2012-6-4]
    /// <para>0=编号,1=标题,2=价格,3=数量,4=小计,5=缩略图,others</para>
    /// </summary>
    public class DemoSessionShoppingCar : SessionDataTable
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="name"></param>
        public DemoSessionShoppingCar(string name = "car")
            : base(name)
        {
            base.SetColumnsName("id", "name", "price", "number", "amount", "thumb");
        }

        /// <summary>
        /// 添加到购物车
        /// <para>0=id,1=title,2=price,3=number,4=amount,5=thumb,others</para>
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="title">名称</param>
        /// <param name="price">价格</param>
        /// <param name="number">数量</param>
        /// <param name="sum">小计金额</param>
        /// <param name="thumb">缩略图</param>
        /// <param name="others">其他信息</param>
        public void Add(object id, string title, decimal price, int number, string thumb, params object[] others)
        {
            IList<object> list = new List<object>();
            list.AddRangeR(id, title, price, number, price * number, thumb);
            if (others.Length > 0) list.AddRangeR(others);
            base.Add(list.ToArray());
        }

        /// <summary>
        /// 获取购物车中的数量总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return base.GetCount(3);
        }

        /// <summary>
        /// 获取指定列的数据的和
        /// </summary>
        /// <returns></returns>
        public double GetSum()
        {
            return base.GetSum(4);
        }

        /// <summary>
        /// 更新数量记录
        /// </summary>
        /// <param name="rowId"></param>
        /// <param name="num"></param>
        /// <param name="price"></param>
        public void UpdateNumberCache(int rowId, int num, decimal price)
        {
            base.UpdateByRowIdCache(rowId, new int[] { 2, 3, 4 }, price, num, num * price);
        }

        public void UpdateNumberCache(string id, int num)
        {
            foreach (DataRow r in this.dataTable.Rows)
            {
                if (r["id"].ToString() == id)
                {
                    r["number"] = num;
                    r["amount"] = num * r["price"].ToString().ToDouble();
                }
            }
        }

    }
}
